/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DeleteMenuList.java
 *
 * Created on Oct 23, 2010, 4:36:38 PM
 */

package cat20032010;

import java.sql.*;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author Daniel Tan
 */
public class DeleteMenuList extends javax.swing.JFrame {
    Statement stmt;
    private ResultSet rset;
    static public String newordername,newordercode,neworderprice;
    public static Vector vecMenu = new Vector();

    String oldcode = "";
    String oldname = "";
    String oldprice ="";
    /** Creates new form DeleteMenuList */
    public DeleteMenuList() {
        initComponents();
        setLocationRelativeTo(null);
        MenuList();
    }
    public void reset(){
        menuname1.setText("");
        menuprice1.setText("");
    }
public void MenuList(){
    vecMenu = new Vector();
    vecMenu.add("Menu Code");
        try {
            Main.getConnection();
            stmt = Main.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rset = stmt.executeQuery("SELECT Menu_Code as Code from Menu");
        } catch (SQLException ex) {
            System.out.println("wrong liao");
        }
        try {
            while (rset.next()) {
                vecMenu.add(rset.getString(1));

            }
        } catch (SQLException ex) {
            System.out.println("Can't Get String");
        }
        MenuCodeBox.setModel(new javax.swing.DefaultComboBoxModel(vecMenu));


    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        menuname1 = new javax.swing.JTextField();
        menuprice1 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel15 = new javax.swing.JLabel();
        MenuCodeBox = new javax.swing.JComboBox();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel2.setLayout(null);

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel8.setText("Delete Menu List");
        jPanel2.add(jLabel8);
        jLabel8.setBounds(165, 25, 176, 42);

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel9.setText("RM");
        jPanel2.add(jLabel9);
        jLabel9.setBounds(230, 180, 30, 30);

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel11.setText("Menu Name");
        jPanel2.add(jLabel11);
        jLabel11.setBounds(60, 140, 110, 30);

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel12.setText("Menu Price");
        jPanel2.add(jLabel12);
        jLabel12.setBounds(60, 180, 110, 30);

        jLabel14.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel14.setText(":");
        jPanel2.add(jLabel14);
        jLabel14.setBounds(180, 140, 40, 30);
        jPanel2.add(menuname1);
        menuname1.setBounds(230, 140, 190, 30);
        jPanel2.add(menuprice1);
        menuprice1.setBounds(260, 180, 160, 30);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/button/OK.png"))); // NOI18N
        jButton2.setBorderPainted(false);
        jButton2.setContentAreaFilled(false);
        jButton2.setDefaultCapable(false);
        jButton2.setFocusPainted(false);
        jButton2.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/button/OKMouseOver.png"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton2);
        jButton2.setBounds(250, 300, 120, 60);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/button/BACK.png"))); // NOI18N
        jButton3.setBorderPainted(false);
        jButton3.setContentAreaFilled(false);
        jButton3.setDefaultCapable(false);
        jButton3.setFocusPainted(false);
        jButton3.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/button/BACKMouseOver.png"))); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton3);
        jButton3.setBounds(377, 300, 130, 60);

        jLabel15.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel15.setText(":");
        jPanel2.add(jLabel15);
        jLabel15.setBounds(180, 180, 40, 30);

        MenuCodeBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        MenuCodeBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                MenuCodeBoxItemStateChanged(evt);
            }
        });
        MenuCodeBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MenuCodeBoxActionPerformed(evt);
            }
        });
        jPanel2.add(MenuCodeBox);
        MenuCodeBox.setBounds(230, 100, 190, 30);

        jLabel16.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel16.setText("Menu Code");
        jPanel2.add(jLabel16);
        jLabel16.setBounds(60, 100, 120, 30);

        jLabel17.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel17.setText(":");
        jPanel2.add(jLabel17);
        jLabel17.setBounds(180, 100, 40, 30);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        if (MenuCodeBox.getSelectedIndex() == 0)
            JOptionPane.showMessageDialog(null, "Menu Code is not Selected", "Error!", JOptionPane.ERROR_MESSAGE);
        else if (menuname1.getText().equals(""))
            JOptionPane.showMessageDialog(null, "Menu Name is not filled", "Error!", JOptionPane.ERROR_MESSAGE);
        else if (menuprice1.getText().equals(""))
            JOptionPane.showMessageDialog(null, "Menu Price is not filled", "Error!", JOptionPane.ERROR_MESSAGE);
        else{
        String MenuCode = (String)this.MenuCodeBox.getSelectedItem();
        try {
            Main.getConnection();
            stmt = Main.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rset = stmt.executeQuery("DELETE FROM Menu where menu_code = '"+MenuCode+"'");
            JOptionPane.showMessageDialog(null, "Deleting Success!!", "Success!", JOptionPane.INFORMATION_MESSAGE);
            Administrator.getdata();
            dispose();
        } catch (SQLException ex) {
            System.out.println("wrong action");
            JOptionPane.showMessageDialog(null, "Menu Could Not Been Deleted", "Error!", JOptionPane.ERROR_MESSAGE);
        }
      }//end of else
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
int reply = JOptionPane.showConfirmDialog(null, "Confirm Cancel Deleting Menu?", "Confirm", JOptionPane.YES_NO_OPTION);
        if (reply == JOptionPane.YES_OPTION){
        	
            dispose();
            reset();
        }     
}//GEN-LAST:event_jButton3ActionPerformed

    private void MenuCodeBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_MenuCodeBoxItemStateChanged
        String MenuCode = (String)this.MenuCodeBox.getSelectedItem();
        if (MenuCode.equals("Menu Code")){
            menuname1.setText("");
            menuprice1.setText("");
        }
 else{
        try {
            Main.getConnection();
            stmt = Main.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rset = stmt.executeQuery("SELECT Menu_Code,Menu_Name,to_char(Menu_Price,'99,999.99') as Menu_Price from Menu where menu_code = '"+MenuCode+"'");
        } catch (SQLException ex) {
            System.out.println("wrong action");
        }

        try{

            while (rset.next()){
                oldcode = rset.getString("Menu_code");
                oldname = rset.getString("Menu_Name");
                oldprice = rset.getString("Menu_Price");
            }
        } catch (SQLException ex) {
            System.out.println("String Not available");
        }
       // menucode.setText(oldcode);
        menuname1.setText(oldname);
        menuprice1.setText(oldprice);
        }
}//GEN-LAST:event_MenuCodeBoxItemStateChanged

    private void MenuCodeBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MenuCodeBoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_MenuCodeBoxActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DeleteMenuList().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox MenuCodeBox;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField menuname1;
    private javax.swing.JTextField menuprice1;
    // End of variables declaration//GEN-END:variables

}
